//
//  DLMyPOP.m
//  Base
//
//  Created by apple on 16/11/8.
//  Copyright © 2016年 dgg. All rights reserved.
//


#import "DLMyPOP.h"

#define kFormat(_text_)   [NSString stringWithFormat:@"%@",_text_]
#define kspringBounciness 15
@implementation DLMyPOP
+(void)myPOP_ScaleAnimationWithNamed:(NSString *)name toValue:(id)toValue fromValue:(id)fromValue view:(id)view popScaleBack:(POPScaleBack)popScaleBack  popBlock:(POPEndBack)popBlock;

{
 
    POPSpringAnimation *positionAnimation = [self myPOP_getScaleAnimationWithNamed:name toValue:toValue fromValue:fromValue popScaleBack:popScaleBack popBlock:popBlock];
    [view pop_addAnimation:positionAnimation forKey:[NSString stringWithFormat:@"positionAnimation_%@",name]];
}

+(void)myPOP_BasicAnimationWithNamed:(NSString *)name toValue:(id)toValue fromValue:(id)fromValue view:(id)view popBasicBack:(POPBasicBack)popBasicBack  popBlock:(POPEndBack)popBlock
{
    POPBasicAnimation *basicAnimation = [self myPOP_getBasicAnimationWithNamed:name toValue:toValue fromValue:fromValue popBasicBack:popBasicBack popBlock:popBlock];
     [view pop_addAnimation:basicAnimation forKey:[NSString stringWithFormat:@"basicAnimation_%@",name]];
}

+(void)myPOP_DecayAnimationWithNamed:(NSString *)name  fromValue:(id)fromValue velocity:(id)velocity view:(id)view popDecayBack:(POPDecayBack)popDecayBack  popBlock:(POPEndBack)popBlock
{
    POPDecayAnimation *decayAnimation = [self myPOP_getDecayAnimationWithNamed:name fromValue:fromValue velocity:velocity popDecayBack:popDecayBack popBlock:popBlock];
     [view pop_addAnimation:decayAnimation forKey:[NSString stringWithFormat:@"decayAnimation_%@",name]];
    
}

+(POPSpringAnimation*)myPOP_getScaleAnimationWithNamed:(NSString *)name toValue:(id)toValue fromValue:(id)fromValue popScaleBack:(POPScaleBack)popScaleBack  popBlock:(POPEndBack)popBlock;
{
    POPSpringAnimation *positionAnimation;
    if ([DLMyPOP isEmpty:name]) {
        positionAnimation = [POPSpringAnimation animation];
    }else
    {
        positionAnimation = [POPSpringAnimation animationWithPropertyNamed:name];

    }
    if (toValue) {
        positionAnimation.toValue = toValue;
    }
 
    if (fromValue) {
        positionAnimation.fromValue = fromValue;
    }
    positionAnimation.springBounciness = kspringBounciness;
    if (popBlock) {
        positionAnimation.completionBlock = popBlock;
    }
    
    if (popScaleBack) {
        positionAnimation = popScaleBack(positionAnimation);
    }
    /*
     positionAnimation.beginTime = CACurrentMediaTime() + 1.0f; //延时调用
     positionAnimation.repeatCount = 5;             //重复次数
     positionAnimation.springSpeed         = 0;     // 设置动画速度(常用)
     positionAnimation.springBounciness    = 20;    // 设置弹性大小(常用)
     positionAnimation.dynamicsFriction    = 10;    // 设置阻止弹性的阻力(选用)
     positionAnimation.dynamicsTension     = 100;   // 设置弹性的张力(可以理解为每次变大的程度, 选用)
     positionAnimation.dynamicsMass        =100      //动画质量 相当于动画做慢动作，，设置之后动画会有很高的质量，但是动画速度很变慢很多 （选用）
     
     */
    
    return positionAnimation;
}

+(POPBasicAnimation*)myPOP_getBasicAnimationWithNamed:(NSString *)name toValue:(id)toValue fromValue:(id)fromValue  popBasicBack:(POPBasicBack)popBasicBack  popBlock:(POPEndBack)popBlock
{
    POPBasicAnimation *basicAnimation;
    if ([DLMyPOP isEmpty:name]) {
        basicAnimation = [POPBasicAnimation animation];
    }else
    {
        basicAnimation = [POPBasicAnimation animationWithPropertyNamed:name];
        
    }
    if (toValue) {
          basicAnimation.toValue = toValue;
    }
  
    if (fromValue) {
        basicAnimation.fromValue = fromValue;
    }
    if (popBlock) {
        basicAnimation.completionBlock = popBlock;
    }
    
    if (popBasicBack) {
        basicAnimation = popBasicBack(basicAnimation);
    }
    /*
     basicAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; //设置动画类型
     basicAnimation.beginTime = CACurrentMediaTime() + 1.0f; //延时调用
     basicAnimation.duration = 0.7;//动画持续时间
     
     */
    return basicAnimation ;

}

+(POPDecayAnimation*)myPOP_getDecayAnimationWithNamed:(NSString *)name  fromValue:(id)fromValue velocity:(id)velocity  popDecayBack:(POPDecayBack)popDecayBack  popBlock:(POPEndBack)popBlock{
    POPDecayAnimation *decayAnimation;
    if ([DLMyPOP isEmpty:name]) {
        decayAnimation = [POPDecayAnimation animation];
    }else
    {
        decayAnimation = [POPDecayAnimation animationWithPropertyNamed:name];
        
    }
    decayAnimation.velocity = velocity;
    if (fromValue) {
        decayAnimation.fromValue = fromValue;
    }
    
    if (popBlock) {
        decayAnimation.completionBlock = popBlock;
    }
    
    if (popDecayBack) {
        decayAnimation = popDecayBack(decayAnimation);
    }
    
    /*
     //decayAnimation.deceleration = 0.998;
     deceleration （负加速度） 很少用到，默认是就是我们地球的 0.998，如果模仿火星那就是0.376 。
     */
    
    return decayAnimation;
    
}

+(void)myPOP_removeAllAnimations:(id)view
{
    [view pop_removeAllAnimations];
}
+(void)myPOP_pushAnimationWithSubtype:(NSString *)subtype  layer:(CALayer*)layer duration:(double)duration
{
    
    CATransition *transition = [CATransition animation];
    transition.type = kCATransitionPush;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.fillMode = kCAFillModeForwards;
    transition.duration = duration;
    transition.subtype = subtype;
    [layer addAnimation:transition forKey:@"UITableViewReloadDataAnimationKey"];

}
//判断是否为空
+ (BOOL) isEmpty:(NSString *)str{
    str = kFormat(str);
    if (str == nil || str == NULL) {
        return YES;
    }
    if ([str isKindOfClass:[NSNull class]]||[str isEqualToString:@""]||[str isEqualToString:@"(null)"]||[str isEqualToString:@"<null>"]||[str isEqualToString:@"null"]||str.length == 0) {
        return YES;
    }
    return NO;
}
@end
